//引入Vue核心库
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
//应用Vuex插件
Vue.use(Vuex)

//引入ajax函数
import { getMyFriends, getMyGroup } from '@/utils/chatBox.js'

//准备actions对象——响应组件中用户的动作
const actions = {
    //获取用户的所有朋友
    async getMyFriends(context) {
        let userMsg = JSON.parse(localStorage.getItem('user'))
        if (userMsg) {
            let { data: { data } } = await getMyFriends(userMsg.qaqUser.id)
            context.commit('setMyFriends', data)
        }
    },
    //获取用户加入的群聊
    async getMyGroup(context) {
        let userMsg = JSON.parse(localStorage.getItem('user'))
        if (userMsg) {
            let { data: { data } } = await getMyGroup(userMsg.qaqUser.id)
            context.commit('setMyGroup', data)
        }
    },
}
//准备mutations对象——修改state中的数据
const mutations = {
    changeType(state, value) {
        state.homeType = value
    },
    //设置用户所有好友
    setMyFriends(state, value) {
        state.userFriends = value
    },
    //设置用户所有群聊
    setMyGroup(state, value) {
        state.userGroup = value
    }
}
//准备state对象——保存具体的数据
const state = {
    homeType: sessionStorage.getItem('type') || 'chatBox',
    userFriends: [],
    userGroup: [],
}

//创建并暴露store
export default new Vuex.Store({
    actions,
    mutations,
    state
})